/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.datawander.i18n;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;

/**
 *
 * @author Marcin Stachniuk
 */
public class I18n {

    public enum LANGUAGE {
        POLISH, ENGLISH;
    }

    private static LANGUAGE lang = LANGUAGE.POLISH;
    private static ResourceBundle resourceBundle = null;
    //private static final Logger logger = Logger.getLogger(I18n.class);;

//    // Set default language
//    static {
//        try {
//            setLanguage(lang);
//        } catch (Exception ex) {
//            logger.fatal(ex,ex);
//            throw new RuntimeException(ex.getMessage());
//        }
//    }

    public static void setLanguage(LANGUAGE language) throws FileNotFoundException, IOException {
        lang = language;
        String userDir;
        userDir = System.getProperty("user.dir");
        
        String filename = userDir+File.separatorChar+"i18n"+File.separatorChar+"messages_";
        //Locale locale;

        switch (lang) {
            case POLISH:
            default:
                //locale = new Locale("pl");
                filename = filename + "pl";
                break;

            case ENGLISH:
                //locale = new Locale("en");
                filename = filename + "en";
                break;
        }
        filename = filename + ".properties";

        resourceBundle = new PropertyResourceBundle(new FileReader(filename));
    }

    public static String getString(String key) {
        if(resourceBundle != null) {
            return resourceBundle.getString(key);
        }
        return "default: "+key;
    }

    public static LANGUAGE getLanguage() {
        return lang;
    }
}

